package org.jsola.contract.web;

import org.jsola.common.StrKit;
import org.jsola.contract.constant.ContractConstants;
import org.jsola.contract.dto.ContractConfigSiteAddDTO;
import org.jsola.contract.dto.ContractConfigSiteUpdateDTO;
import org.jsola.contract.entity.ContractConfigSiteDO;
import org.jsola.contract.query.ContractConfigSiteQuery;
import org.jsola.contract.service.IContractConfigSiteService;
import org.jsola.contract.vo.ContractConfigSiteListVO;
import org.jsola.contract.vo.ContractConfigSiteVO;
import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import javax.validation.Valid;


/**
 * 租户电子合同配置
 *
 * @author JanusMix
 */
@RestController("contractContractConfigSiteController")
@RequestMapping(ContractConstants.API_PATH + "/v1/contractConfigSites")
public class ContractConfigSiteController implements IContractConfigSiteControllerAPI {

    @Autowired
    private IContractConfigSiteService contractConfigSiteService;

    @Override
    @PostMapping
    public Result<ContractConfigSiteVO> save(@Valid @RequestBody ContractConfigSiteAddDTO contractConfigSiteAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增租户电子合同配置权限校验

        return Result.success(
                contractConfigSiteService.save(contractConfigSiteAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody ContractConfigSiteUpdateDTO contractConfigSiteUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改租户电子合同配置权限校验
        if (StrKit.isNotEmpty(contractConfigSiteUpdateDTO.getSiteId())) {
            tokenUser.setSiteId(contractConfigSiteUpdateDTO.getSiteId());
        }

        int count = contractConfigSiteService.update(contractConfigSiteUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{contractConfigSiteId}")
    public Result<Integer> delete(@PathVariable Long contractConfigSiteId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除租户电子合同配置权限校验

        int count = contractConfigSiteService.deleteByIds(tokenUser, contractConfigSiteId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody Long[] contractConfigSiteIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除租户电子合同配置权限校验

        int count = contractConfigSiteService.deleteByIds(tokenUser, contractConfigSiteIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{contractConfigSiteId}")
    public Result<ContractConfigSiteVO> findById(@PathVariable Long contractConfigSiteId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看租户电子合同配置权限校验

        return Result.success(
            contractConfigSiteService.selectById(contractConfigSiteId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<ContractConfigSiteListVO>> listAll(@Valid ContractConfigSiteQuery contractConfigSiteQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看租户电子合同配置权限校验

        return Result.success(
            contractConfigSiteService.select(contractConfigSiteQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<ContractConfigSiteListVO>> page(@Valid ContractConfigSiteQuery contractConfigSiteQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            contractConfigSiteService.selectPage(contractConfigSiteQuery, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping("/selectBySiteId")
    public Result<ContractConfigSiteVO> selectBySiteId() {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        ContractConfigSiteDO contractConfigSiteDO = contractConfigSiteService.selectBySiteId(tokenUser.getSiteId());
        ParamException.notNull(contractConfigSiteDO, "没有查询到租户的合同配置信息，请联系管理员添加配置信息！");
        return Result.success(
                contractConfigSiteDO.to(ContractConfigSiteVO.class)
        );
    }


}

